<template>
    <div class="management1">
        <header>
            <div class="title">
                <fourLevelLinkage
                    @changeProvince="changeProvince"
                    @changeCity="changeCity"
                    @changeArea="changeArea"
                    @changeHospital="changeHospital"
                    :province="province"
                    :city="city"
                    :area="area"
                    :hospital="hospital"
                ></fourLevelLinkage>
            </div>
            <Input
                v-model.trim="val"
                placeholder="机构名称"
                style="width: 200px;margin-right:10px;"
                clearable
            />
            <Button type="primary" icon="ios-search" @click="search"></Button>
        </header>
        <Table stripe :columns="columns1" :data="tableList"></Table>
        <div style="text-align:center;margin:10px 0;">
            <Page :total="homeSize" show-total show-elevator :current='pageNo' @on-change="pageChange"/>
        </div>
    </div>
</template>

<script>
import api from "@/api/commonApi";
import fourLevelLinkage from "@/components/fourLevelLinkage";
import aesUtils from "@/plugins/aes-utils.js";
import store from "@/store";
export default {
    components: {
        fourLevelLinkage
    },
    data() {
        return {
            province: null,
            city: null,
            area: null,
            hospital: null,
            tableList: [],
            val: "",
            homeSize: 10,
            pageSize: 10,
            pageNo:1,
            columns1:[
                {
                    title: '排序',
                    key: 'sum',
                    align: 'center',
                    width: 60,
                    render: (h, params) => {
                        let sum = (this.pageNo-1)*this.pageSize + params.index +1;
                        return h("span", sum);
                    }
                },
                {
                    title: '区域',
                    key: 'provinceName',
                    align: 'center',
                    // width: 80,
                },
                {
                    title: '机构名称',
                    key: 'orgName',
                    align: 'center',
                    // width: 150,
                },
                {
                    title: '互联网医院',
                    key: 'internetHospital',
                    align: 'center',
                    // width: 150,
                    render: (h, params) => {
                        let internetHospital = params.row.internetHospital;
                        let name;
                        if (internetHospital == 1) {
                        name = "开通";
                        } else  {
                        name = "未开通";
                        }
                        return h("span", {}, name);
                    }
                },
                {
                    title: '处方流转',
                    key: 'ipres',
                    align: 'center',
                    // width: 120,
                    render: (h, params) => {
                        let ipres = params.row.ipres;
                        let name;
                        if (ipres == 0) {
                        name = "已关闭";
                        } else if (ipres == 1) {
                        name = "已开通";
                        } else if (ipres == null) {
                        name = "未开通";
                        }
                        return h("span", {}, name);
                    }
                },
                {
                    title:"远程门诊",
                    key:"iremote",
                    align: 'center',
                    // width: 120,
                    render: (h, params) => {
                        let iremote = params.row.iremote;
                        let name;
                        if (iremote == 1) {
                        name = "开通";
                        } else  {
                        name = "未开通";
                        }
                        return h("span", {}, name);
                    }
                },
                {
                    title:"远程会诊",
                    key:"nick",
                    align: 'center',
                    // width: 120,
                    render: (h, params) => {
                        let iremoteConsult = params.row.iremoteConsult;
                        let name;
                        if (iremoteConsult == 1) {
                        name = "开通";
                        } else  {
                        name = "未开通";
                        }
                        return h("span", {}, name);
                    }
                },
                {
                    title:"医院主公众号",
                    key:"nick",
                    align: 'center',
                    // width: 120,
                },
                {
                    title:"设置",
                    align: 'center',
                    width: 120,
                    fixed:'right',
                    render: (h,params) => {
                        let a = params.row
                        return h('div', [
                            h('span', {
                                style: {
                                    color: " #5362e1",
                                },
                                on: {
                                    click: () => {
                                        this.navto(a)
                                    }
                                }
                            }, '编辑'),
                            ' | ',
                            h('span', {
                              style: {
                                color: "#FEA443",
                              },
                              on: {
                                click: () => {
                                  this.updateCache(a)
                                }
                              }
                            }, '更新缓存'),
                        ])
                    }
                }
            ]
        };
    },
    created() {
        let breadList = [
            { path: "/index", title: "首页" },
            {
                path: "/index/operation/mechanism/index",
                title: "机构运营"
            },
            {
                path: "/index/operation/home",
                title: "机构运营配置管理"
            }
        ];
        this.$emit("changeBreadList", breadList);
    },
    mounted() {
        this.getDate(1);
    },
    methods: {
        changeProvince(val) {
            this.province = val;
        },
        changeCity(val) {
            this.city = val;
        },
        changeArea(val) {
            this.area = val;
        },
        changeHospital(val) {
            this.hospital = val;
        },
        // 获取医院列表
        getDate(pageNo, val) {
            this.pageNo = pageNo;
            let params = {
                pageNo,
                pageSize: 10
            };
            if (Boolean(val)) {
                params.orgName = val.trim();
            }
            params.provinceCode = this.province ? this.province : null;
            params.cityCode = this.city ? this.city : null;
            params.districtCode = this.area ? this.area : null;
            params.hospitalId = this.hospital ? this.hospital : null;
            this.$axios
                .post(api.mechanismregList, params)
                .then(res => {
                    if (res.data.code) {
                        let ret = res.data.object.records;
                        this.homeSize = res.data.object.total;
                        if(ret){
                            ret.forEach((item,index) =>{
                                item.sum = this.addZeros(index)
                                item.hospitalType = item.hospitalType == 1 ? '医院' :""
                            })
                            this.tableList = ret;
                        } else {
                            this.tableList = []
                        }
                    } else {
                        this.$Message.error("没有访问权限");
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        pageChange(index) {
            this.getDate(index, this.val);
        },
        navto(item) {
            let iv = store.state.iv;
            let salt = store.state.salt;
            sessionStorage.setItem("hospitalId", item.id);
            sessionStorage.setItem("cityCode", item.cityCode);
            sessionStorage.setItem("districtCode", item.districtCode);
            sessionStorage.setItem("provinceCode", item.provinceCode);
            sessionStorage.setItem("orgCode", item.orgCode);
            sessionStorage.setItem("hospitalName", item.orgName);
            sessionStorage.setItem("provinceName", item.provinceName);
            sessionStorage.setItem("cityName", item.cityName);
            sessionStorage.setItem("areaName", item.districtName);
            localStorage.setItem(
                "hospitalName",
                aesUtils.encrypt(salt, iv, "Doctortoservice", item.orgName)
            );
            this.$router.push({
                name: "homeinfo"
            });
            // this.functionJS.queryNavgationTo(this, "/index/organizat/contentmen/homeinfo/home_info", {
            //     cityCode: item.cityCode,
            //     districtCode: item.districtCode,
            //     provinceCode: item.provinceCode,
            // });
        },
        search() {
            this.getDate(1, this.val);
        },
        //更新缓存
        updateCache(item){
          let params = {};
          params.id = item.id;
          this.$axios
            .post(api.updateHospitalCache, params)
            .then(resp => {
              if (resp.data.success) {
                this.$Message.info("更新成功");
              } else {

              }
            })
            .catch(err => {
              // this.$Message.info("服务器超时，请重试");
            });
        }
    }
};
</script>

<style scoped lang = "less">
.management1 {
    width: 100%;
    padding:10px;
    background: #fff;
    header {
        width: 100%;
        display: flex;
        flex-direction: row;
        margin: 10px 0;
        span {
            color: black;
            line-height: 30px;
            margin-right: 10px;
        }
        .title{
            margin-right: 5px;
        }
    }
}
</style>
